﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace AIC_CD
{
    public partial class UCBaoCaoXepLoaiCongDoanCoSo : MyUserBase
    {
        private PrepareDataForTreeView DataForTreeView = new PrepareDataForTreeView();
        private static int IDDonVi;
        private static int KyBaoCao;

        protected void Page_Load(object sender, EventArgs e)
        {
            lblNotice.Visible = false;
            CheckPermission();
            if (!IsPostBack)
            {
                BindDataToDropDownList(); ;
                DataForTreeView.PopulateRootLevel(trvDonVi);
            }
            KyBaoCao = Convert.ToInt32(ddlKyBaoCao.SelectedValue);
        }

        /// <summary>
        /// Thủ tục kiểm tra quyền sử dụng của người dùng có được quyền truy cập vào mục này hay không?
        /// </summary>
        private void CheckPermission()
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission("BaoCaoXepLoaiCongDoanCoSo");
            if (Role == null || Role["Xem"] == 0)
            {
                Response.Redirect("Index.aspx?p=0");
            }
        }

        protected void trvDonVi_SelectedNodeChanged(object sender, EventArgs e)
        {
            lblDonVi.Text = "Đơn vị đã chọn : " + trvDonVi.SelectedNode.Text;
            IDDonVi = Convert.ToInt32(trvDonVi.SelectedValue);
        }

        protected void trvDonVi_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            DataForTreeView.PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
        }

        protected void BindDataToDropDownList()
        {
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, "SELECT * FROM dbo.tblDanhMuc WHERE MaDM='KyBaoCao'");
            ddlKyBaoCao.DataSource = dt;
            ddlKyBaoCao.DataTextField = "TenDM";
            ddlKyBaoCao.DataValueField = "ID";
            ddlKyBaoCao.DataBind();
        }

        protected void btnXemBaoCao_Click(object sender, EventArgs e)
        {
            if (trvDonVi.SelectedNode == null)
            {
                lblNotice.Visible = true;
                lblNotice.InnerText = "Vui lòng lựa chọn đơn vị muốn xem báo cáo";
            }
            else
            {
                List<SqlParameter> lstParams = new List<SqlParameter>();
                if (trvDonVi.SelectedNode.ChildNodes.Count == 0)
                {
                    lstParams.Add(SqlHelper.CreateParameterObject("@KyBaoCao", SqlDbType.Int, ParameterDirection.Input, KyBaoCao));
                    lstParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, IDDonVi));
                    MyReportSQL = "SELECT * FROM dbo.tblBaoCaoXepLoaiCongDoanCoSo WHERE KyBaoCaoID_DM = @KyBaoCao AND DonViID = @DonViID And IsCommon<>1";
                }
                else
                {
                    if (chkTongHop.Checked)
                    {
                        lstParams.Add(SqlHelper.CreateParameterObject("@KyBaoCao", SqlDbType.Int, ParameterDirection.Input, KyBaoCao));
                        lstParams.Add(SqlHelper.CreateParameterObject("@parentID", SqlDbType.NVarChar, ParameterDirection.Input, IDDonVi + ";"));
                        lstParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, IDDonVi));
                        MyReportSQL = "select DM.TenDM AS NoiDung, " +
                                        "SUM(bc.F1) AS F1,SUM(bc.F2) AS F2,SUM(bc.F3) AS F3,SUM(bc.F4) AS F4,SUM(bc.F5) AS F5, " +
                                        "SUM(bc.F6) AS F6,SUM(bc.F7) AS F7,SUM(bc.F8) AS F8,SUM(bc.F9) AS F9,SUM(bc.F10) AS F10, " +
                                        "SUM(bc.F11) AS F11,SUM(bc.F12) AS F12,SUM(bc.F13) AS F13,SUM(bc.F14) AS F14,'' AS Ghichu " +
                                        "From dbo.tblBaoCaoXepLoaiCongDoanCoSo bc " +
                                        "inner join tblDanhMuc DM on DM.ID = bc.NoiDungMau1ID_DM " +
                                        "where bc.KyBaoCaoID_DM = @KyBaoCao " +
                                        "AND bc.DonViID in " +
                                        "(Select ID from tblDonVi where parentID like '%'+@parentID+'%') " +
                                        "Group By TenDM";
                    }
                    else
                    {
                        lstParams.Add(SqlHelper.CreateParameterObject("@KyBaoCao", SqlDbType.Int, ParameterDirection.Input, KyBaoCao));
                        lstParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, IDDonVi));
                        MyReportSQL = "SELECT * FROM dbo.tblBaoCaoXepLoaiCongDoanCoSo WHERE KyBaoCaoID_DM = @KyBaoCao AND DonViID = @DonViID And IsCommon<>1";
                    }
                }
                MyReportParams = lstParams;
                MyReportSQLParam = "SELECT DV.TenDV, BC.CM_TenBaoCao, BC.CM_NguoiLapBieu, BC.CM_ThuTruong FROM dbo.tblBaoCaoXepLoaiCongDoanCoSo BC, tblDonVi DV WHERE BC.DonViID=DV.ID And BC.KyBaoCaoID_DM = @KyBaoCao AND BC.DonViID = @DonViID And BC.IsCommon=1"; ;
                MyReportName = "rptBaoCaoXepLoaiCongDoanCoSoTheoNhiemKy.rpt";
                MyTableName = "tblRptAll";
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), null, "window.open('MyReportView.aspx');", true);


            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("index.aspx");
        }

    }
}